//
//  ZZMainTabBar.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/9.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit

class ZZMainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        //在重构方法中添加加号控件
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUpUi()
    }

    private func setUpUi() {
        
        addSubview(composeBtn)
        
    }
    
    //设置 UITabBarButton 的布局
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        var index: CGFloat = 0
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRectOffset(rect, index * w, 0)
                
                index += index == 1 ? 2 : 1
            }
            
        }
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        //将指定的控件推到前面
        bringSubviewToFront(composeBtn)
    }
    
    //懒加载控件
    lazy var composeBtn : UIButton = UIButton(title: nil, image: "tabbar_compose_icon_add", backImage: "tabbar_compose_button", color: nil)
    
}
